package com.xiaoming.stream;

import com.alibaba.fastjson.JSONObject;
import com.xiaoming.adapter.MessageAdapter;
import com.xiaoming.constants.Constants;
import com.xiaoming.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

/**
 * @author xiaoming
 * @Date 2020/3/11
 */
@Component
@Slf4j
public class ConsumerDistribute {

    @Autowired
    private EmailService emailService;

    private MessageAdapter messageAdapter;

    @StreamListener("mall_member_channel")
    public void distribute(String json) {
        log.info("####ConsumerDistribute###distribute() 消息服务平台接受 json参数:" + json);
        if (StringUtils.isEmpty(json)) {
            return;
        }
        JSONObject jsonObecjt = JSONObject.parseObject(json);
        JSONObject header = jsonObecjt.getJSONObject("header");
        String interfaceType = header.getString("interfaceType");
        if (StringUtils.isEmpty(interfaceType)) {
            return;
        }

        if (interfaceType.equals(Constants.SMS_MAIL)) {
            messageAdapter = emailService;
        }

        if (messageAdapter == null) {
            return;
        }
        JSONObject body = jsonObecjt.getJSONObject("content");
        messageAdapter.sendMsg(body);
    }
}
